package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://www.56.com/ 56网
 * @author allen
 */
public class N56Analyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://www.56.com/.*/v.*.html",
				"http://www.56.com/.*/.*aid.*vid.*.html",
				"http://www.56.com/redian/[A-Za-z0-9]+/[A-Za-z0-9]+.html",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = null;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("div#VideoTitle div.title3 h1").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("div.ablum_title_top h3").text();
			if(StringUtils.isBlank(title)){
				title=doc.select(".re_top_title").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("title").text();
					title=StringUtils.substringBefore(title, "_");
					title=StringUtils.substringBefore(title, "-");
				}
			}
		}
		desc=doc.select("#vSummary").text();
		if(desc!=null){
			desc=StringUtils.substringAfter(desc, "简介：");
		}

		if(StringUtils.isBlank(desc)){
			desc=doc.select("div.title_content div.ablum_desc p.gray_txt").text();
		}

		String tempdate=doc.select("#pub_date").text();
		if(tempdate==null || tempdate.trim().length()==0){
			if(htmltxt.indexOf("\"save_time\":\"")!=-1){
				tempdate=htmltxt.substring(htmltxt.indexOf("\"save_time\":\"")+"\"save_time\":\"".length());
				if(tempdate.indexOf("\"")!=-1){
					tempdate=tempdate.substring(0,tempdate.indexOf("\""));
					try {
						date=Long.parseLong(tempdate)*1000;
					} catch (Exception e) {
					}
				}
			}
			tempdate=doc.select("#pub_date").text();
		}else{
			date = DateUtils.matchDate(tempdate);
		}
		video.setTitle(StringUtils.trimSpace(title));
		video.setContent(desc);
		if(date==null){
			date = DateUtils.matchDate(doc.select("div.video_info div.info_list ul li span,.create_date").text());
			if(date==null){
				date = DateUtils.matchDate(doc.select("div#vSummary").text());
			}
		}
		video.setDate(date);
		if(StringUtils.isBlank(title)){
		}

		String author=doc.select("p.user_name_b a").text();
		if(StringUtils.isBlank(author)){
			author = doc.select("p.creator_name a").text();
		}
		video.setAuthor(author);
	    return video;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) throws Exception {
		N56Analyse a = new N56Analyse();
		String url = "http://www.56.com/u91/v_MTM3NzczNjcy.html";
		System.out.println(a.isDetailPage(url));
		UrlMeta meta = CrawlHTML.responseToURL(url);
		NewsMeta parserHtml = a.parserHtml(meta);
		System.out.println(parserHtml);
	}


	
}
